Skip to content

fix(restore): strip database-redirect directives from plain SQL before restore#43

Merged
eduardozaniboni merged 1 commit intomasterfrom
fix/restore-strip-sql-directives
Mar 10, 2026
Merged

fix(restore): strip database-redirect directives from plain SQL before restore#43
eduardozaniboni merged 1 commit intomasterfrom
fix/restore-strip-sql-directives

Conversation

@eduardozaniboni
Copy link
Copy Markdown
Collaborator

Type

  • fix — bug fix

Description

Plain SQL files produced by pg_dump -C, pgAdmin, DBeaver, or mysqldump --databases embed directives that redirect the database client to the source database, overriding the --db target:

  • PostgreSQL: \connect <db> / \c <db> meta-commands
  • MySQL: USE <db>; statement
  • Both: CREATE DATABASE <db>; / DROP DATABASE <db>; blocks

When a user ran hdx restore sateus.sql --db nfse_ce_eusebio, the CLI correctly created nfse_ce_eusebio, but the data was restored into sateus (the database named in the SQL file).

Related issues: N/A


Changes

  • src/infra/engines/sql-filter.ts — new helper filterSqlDirectives() that strips the offending directives line by line, handling both single-line and multi-line blocks
  • src/infra/command-runner.ts — added stdinContent?: string to RunOptions (maps to execa input option)
  • src/infra/engines/postgres/postgres.engine.ts — plain SQL restore: filter content and pipe via stdin instead of -f file
  • src/infra/engines/mysql/mysql.engine.ts — restore: filter content and pipe via stdin instead of inputFile
  • tests/unit/infra/sql-filter.test.ts — 10 unit tests for filterSqlDirectives()
  • package.json, README.md, README.pt-BR.md, CHANGELOG.md — version 0.9.0 → 0.9.1

How to Test

  1. Obtain a SQL dump created with pg_dump -C mydb > mydb.sql or pgAdmin export
  2. Run: hdx restore mydb.sql --db other_db
  3. Confirm data appears in other_db, not in mydb
  4. Run npm run test:unit — all 520 tests pass

Quality Checklist

Always required:

  • Follows architecture and layer rules (AGENTS.md)
  • Formatted with npm run lint:fix
  • Branch is off master, not a direct commit to it

Required for feat / fix / refactor:

  • Commands remain engine-agnostic (no psql, mysql, or binary names in src/commands/)
  • Unit tests pass: npm run test:unit
  • Integration tests pass: npm run test:integration

Required if engines or commands were changed:

  • E2E tests pass for the affected engine(s): verified locally with test:e2e:pgsql

Required if user-facing behavior changed:

  • README.md and README.pt-BR.md updated accordingly

…e restore (v0.9.1)

pg_dump -C, pgAdmin and mysqldump --databases embed directives (\connect,
CREATE DATABASE, USE) that redirect the client to the source database,
overriding the --db target provided by the user. The result was that the
data was restored to the source database instead of the intended one.

Fix: read the plain SQL file into memory, strip the offending directives
via filterSqlDirectives(), then pipe the sanitised content via stdin.
This works for both internal herdux backups (which never include these
directives) and for external SQL files.

Affects: PostgreSQL plain restore (psql), MySQL restore.
New helper: src/infra/engines/sql-filter.ts (10 unit tests).
@eduardozaniboni eduardozaniboni merged commit f506b5c into master Mar 10, 2026
7 checks passed
@eduardozaniboni eduardozaniboni deleted the fix/restore-strip-sql-directives branch March 10, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant